package com.sg.business.work.handler;

import java.util.Map;

import org.eclipse.core.commands.Command;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;

import com.mobnut.db.model.ModelService;
import com.mobnut.db.model.PrimaryObject;
import com.sg.bpm.workflow.WorkflowService;
import com.sg.business.model.DummyModel;
import com.sg.business.model.UserTask;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.command.AbstractNavigatorHandler;
import com.sg.widgets.part.editor.DataObjectDialog;
import com.sg.widgets.viewer.ViewerControl;

public class ActorsEditor extends AbstractNavigatorHandler {

	@Override
	protected void execute(PrimaryObject selected, IWorkbenchPart part,
			ViewerControl vc, Command command, Map<String, Object> parameters,
			IStructuredSelection selection) {
		if (selected instanceof UserTask) {
			try {
				DummyModel dy = ModelService
						.createModelObject(DummyModel.class);
				DataObjectDialog dialog = DataObjectDialog.openDialog(dy,
						"editor.allwork.actors", true, null);
				int rc = dialog.getReturnCode();
				if (rc == DataObjectDialog.OK) {
					UserTask userTask = (UserTask) selected;
					long taskId = userTask.getTaskId();
					String userId = userTask.getUserId();
					String targetUserId = (String) dy.getValue("actorid");
					WorkflowService.getDefault().delegateTask(taskId, userId,
							targetUserId);
					WorkflowService.getDefault()
							.claimTask(taskId, targetUserId);
					userTask.doSetDelegator(targetUserId);
				}

			} catch (Exception e) {
				MessageUtil.showToast(e);
			}

		}
	}

}
